﻿using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using zichan.BLL;
using zichan.Entity;

namespace zichan.Jobs
{
    public class StaffJob:IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                WebClient client = new WebClient();
                client.Encoding = System.Text.Encoding.UTF8;
                string result = client.DownloadString("http://oa.xmisp.com/tree/jsoncode");
                List<Item> items = JsonConvert.DeserializeObject<List<Item>>(result);
                // WriteLog(JsonConvert.SerializeObject(items));
                List<Staff> curstaffs = StaffService.StaffAll(1);//当前职员

                List<Staff> adds = ConventStaff(items);
                List<Staff> edits = new List<Staff>(); //要修改的数据

                foreach (Staff staff in curstaffs)
                {
                    Staff s = adds.FirstOrDefault(o => o.Id == staff.Id);// && o.Name == staff.Name && o.DepartmentName == staff.DepartmentName);
                    if (s != null)
                    {
                        if (s.Name != staff.Name || s.DepartmentName != staff.DepartmentName || s.Email != staff.Email)
                            edits.Add(s);
                        adds.Remove(s);
                    }
                    else
                    {
                        staff.Status = 2;
                        edits.Add(staff);
                    }
                }
                StaffService.InsUptStaff(adds, edits);

                //WriteLog(JsonConvert.SerializeObject(adds) + "|" + JsonConvert.SerializeObject(edits));
            }
            catch (Exception exc)
            {

            }
        }




        List<Staff> ConventStaff(List<Item> items)
        {
            List<Item> newitems = items.Where(o => string.IsNullOrEmpty(o.nocheck)).ToList();
            List<Item> pitems = items.Where(o => o.nocheck == "true").ToList();

            List<Staff> staffs = new List<Staff>();
            foreach (Item item in newitems)
            {
                if (string.IsNullOrEmpty(item.pId))
                    continue;

                Item pit = pitems.FirstOrDefault(o => o.id == item.pId);
                string DepartmentName = pit.name;
                Item pit2 = null;
                if (pit.pId != "-1" && !string.IsNullOrEmpty(pit.pId))
                {
                    pit2 = pitems.FirstOrDefault(o => o.id == pit.pId);
                    DepartmentName = pit2.name + "-" + DepartmentName;
                }

                string[] ids = item.id.Split(new char[] { '-' }, StringSplitOptions.None);
                if (ids[1] == "1")
                    continue;

                staffs.Add(new Staff { Id = Convert.ToInt32(ids[1]), Name = item.name, DepartmentName = DepartmentName, Status = 1, Email = ids[2] });
            }
            return staffs;
        }

        Task IJob.Execute(IJobExecutionContext context)
        {
            throw new NotImplementedException();
        }

        class Item
        {
            public string id { get; set; }
            public string name { get; set; }
            public string nocheck { get; set; }
            public string pId { get; set; }
        }
    }
}
